<?php 
$peer = Product2photoPeer::getInstance();
$id = Zend_Controller_Front::getInstance()->getRequest()->getParam('id');
$select = $peer->select();
$select->where(Product2photoPeer::PRODUCT_ID, $id);
$rowset = $peer->fetchAll($select);

foreach ($rowset as $row) {
	$photo = $row->getPhotoRowByPhotoId();
	$value = $photo->getFilePreview();
	
	$file = File::factory($value);
	
	echo "
	<a href='{$file->getHtdocsPath()}'>
		<img src='{$file->getHtdocsPath()}' width='100' height='100' />
	</a>
	&nbsp;<a href='#' row_id='{$row->getId()}' class='photo_delete'>[x]</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	
}


$element = $this->element;

$element->clearDecorators();
$element->loadDefaultDecorators();
echo $element;
?>

<script>

var $countOfPhotosAdded = 1;
$(function() {
	var $dd = $("#photo_1-label");
	var $label = $dd.find('label');

	//debugger;
	$label.html($label.text() + "<a href='#' id='add_photo'>[ + ]</a>");

	
	$("#add_photo").live("click", function() {
		var $photo = $("#photo_1");
		var $element = $("#photo_1-element");

		$countOfPhotosAdded++;
		var $clone = $("<input type='file' id='photo_"+$countOfPhotosAdded+"' name='photo_"+$countOfPhotosAdded+"' />");
		$element.append("<br />");
		$element.append($clone);

		return false;
	});


	$(".photo_delete").live("click", function() {
		if (!confirm("Вы уверены, что хотите удалить фото ?")) return false;
		$self = $(this);
		var $url = '/admin/product/delete-photo/id/' + $self.attr('row_id');
		$.ajax({
			url: $url,
			type: 'post',
			dataType: 'json',
			success: function($data) {
				if ($data.result == 'success') {
					$self.prev().remove();
					$self.remove();
				} else {
					alert($data.message);
				}
			}
		});

		return false;
	});
	
	
});
</script>
